package ge.telasi.tasks.ui.task;

import ge.telasi.tasks.model.Task;
import ge.telasi.tasks.model.User;
import ge.telasi.tasks.search.Condition;
import ge.telasi.tasks.ui.Platform;
import ge.telasi.tasks.ui.UIUtils;
import ge.telasi.tasks.ui.comp.ListModel;
import ge.telasi.tasks.ui.search.BooleanField;
import ge.telasi.tasks.ui.search.DateField;
import ge.telasi.tasks.ui.search.TaskStatusField;
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.JList;

/**
 * @author dimitri
 */
public class StandardFolderList extends JList {

    public StandardFolderList() {
        setOpaque(false);
        setBorder(null);
        ListModel<FolderEntry> model = new ListModel<FolderEntry>();
        model.add(new InboxEntry());
        model.add(new OutboxEntry());
        model.add(new OpenEntry());
        model.add(new ClosedEntry());
        model.add(new NewEntry());
        model.add(new FocusEntry());
        model.add(new AllEntry());
        setModel(model);
        setCellRenderer(new FolderRenderer());
    }

    class FolderRenderer extends DefaultListCellRenderer {
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            setOpaque(isSelected);
            setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            if (value instanceof FolderEntry) {
                FolderEntry folderEntry = (FolderEntry) value;
                setText(folderEntry.getName());
                setIcon(folderEntry.getImage());
            }
            return this;
        }
    }

    public interface FolderEntry {
        String getName();
        Icon getImage();
        List<Condition> getConditions();
    }

    /**
     * შემომავალი დავალებები.
     */
    class InboxEntry implements FolderEntry {
        public String getName() {
            return "შემომავალი (ღია)";
        }

        public Icon getImage() {
            return UIUtils.getIcon16x16("email.png");
        }

        public List<Condition> getConditions() {
            List<Condition> conditions = new OpenEntry().getConditions();
            User user = Platform.getDefault().getUser();
            Condition cond = new Condition();
            cond.setCondition("${taskSearch.receiver_user} = :user_id");
            cond.getSubstitutions().put("user_id", user.getId());
            conditions.add(cond);
            return conditions;
        }
    }

    /**
     * გამავალი დავალებები.
     */
    class OutboxEntry implements FolderEntry {

        public String getName() {
            return "გამავალი (ღია)";
        }

        public Icon getImage() {
            return UIUtils.getIcon16x16("email_go.png");
        }

        public List<Condition> getConditions() {
            List<Condition> conditions = new OpenEntry().getConditions();
            User user = Platform.getDefault().getUser();
            Condition cond = new Condition();
            cond.setCondition("${taskSearch.owner} = :user_id");
            cond.getSubstitutions().put("user_id", user.getId());
            conditions.add(cond);
            return conditions;
        }

    }

    class OpenEntry implements FolderEntry {

        public String getName() {
            return "ყველა ღია";
        }

        public Icon getImage() {
            return UIUtils.getIcon16x16("application.png");
        }

        public List<Condition> getConditions() {
            List<Integer> statuses = new ArrayList<Integer>();
            statuses.add(Task.STATUS_DRAFT);
            statuses.add(Task.STATUS_OPEN);
            statuses.add(Task.STATUS_PAUSED);
            statuses.add(Task.STATUS_COMPLETE_OK);
            statuses.add(Task.STATUS_COMPLETE_CANCEL);
            TaskStatusField statusField = new TaskStatusField();
            statusField.setStatuses(statuses);
            List<Condition> conditions = new ArrayList<Condition>();
            conditions.add(statusField.getCondition());
            return conditions;
        }
    }

    class ClosedEntry implements FolderEntry {

        public String getName() {
            return "დახურული";
        }

        public Icon getImage() {
            return UIUtils.getIcon16x16("accept.png");
        }

        public List<Condition> getConditions() {
            List<Integer> statuses = new ArrayList<Integer>();
            statuses.add(Task.STATUS_CLOSED_OK);
            statuses.add(Task.STATUS_CLOSED_CANCEL);
            TaskStatusField statusField = new TaskStatusField();
            statusField.setStatuses(statuses);
            List<Condition> conditions = new ArrayList<Condition>();
            conditions.add(statusField.getCondition());
            return conditions;
        }
        
    }

    class NewEntry implements FolderEntry {

        public String getName() {
            return "ახალი";
        }

        public Icon getImage() {
            return UIUtils.getIcon16x16("new.png");
        }

        public List<Condition> getConditions() {
            List<Condition> conditions = new ArrayList<Condition>();
            BooleanField newCondition = new BooleanField("taskSearch.newTask", "ახალი");
            newCondition.setFlag(true);
            conditions.add(newCondition.getCondition());
            return conditions;
        }

    }

    class FocusEntry implements FolderEntry {

        public String getName() {
            return "სასწრაფო";
        }

        public Icon getImage() {
            return UIUtils.getIcon16x16("exclamation.png");
        }

        public List<Condition> getConditions() {
            List<Condition> conditions = new ArrayList<Condition>();
            // only OPEN status
            List<Integer> statuses = new ArrayList<Integer>();
            statuses.add(Task.STATUS_OPEN);
            TaskStatusField statusField = new TaskStatusField();
            statusField.setStatuses(statuses);
            conditions.add(statusField.getCondition());
            // date should be in desirable interval (-INFINITY, +2)
            DateField field = new DateField("taskSearch.dueDate", "ბოლო თარიღი");
            field.setMethod(DateField.INTERVAL);
            field.setSubMethod(DateField.BY_COUNT);
            field.setCount1(-1000000);
            field.setCount2(2);
            conditions.add(field.getCondition());
            // all conditions
            return conditions;
        }

    }

    class AllEntry implements FolderEntry {

        public String getName() {
            return "ყველა";
        }

        public Icon getImage() {
            return UIUtils.getIcon16x16("folder_page.png");
        }

        public List<Condition> getConditions() {
            return null;
        }
        
    }

}
